/**
 * Created with JetBrains PhpStorm.
 * User: jiangshuguang
 * Date: 14-10-28
 * Time: 下午2:47
 * To change this template use File | Settings | File Templates.
 */
(function(){
    var Dialog =  require('home:widget/ui/dialog/dialog.js').Dialog,
        $page = $(".page-iphone6assets"),
        $headerContent = $page.find(".header-content"),
        $fundList = $page.find('.fund-list'),
        $bankList = $page.find(".bank-list");

    P.createPage({
       _data:{},
        _create:function(){
            this.data({
                dialogTip:new Dialog({width:400}),
                dialog:new Dialog({
                    width:400,
                    confirmBtn:true,
                    content:''
                })
            })
        },
        _init:function(){
            var me = this,
                dialogTip = this.data('dialogTip'),
                dialog = this.data('dialog');

            $headerContent.find('.btn-modifybank,.btn-addbank').click(function(e){
                e.stopPropagation();
                $bankList.html('正在获取...').show();
                 me.showBankList();
            });

            $fundList.find('.btn-enable').click(function(e){

                if($(this).hasClass('btn-enable')){
                    var me = this,
                        bankCard = $headerContent.find('.item3 .data-value').text();
                    if(!bankCard){
                        dialogTip.show('<div style="text-align: center;line-height: 50px;">未绑定银行卡 ,请先绑定银行卡</div>');
                    }else{
                        dialog.show('' +
                            '<div style="text-align: center;line-height: 30px">' +
                            '<div>您绑定的银行卡为'+bankCard+'</div>' +
                            '<div>确定要提现？</div>' +
                            '</div>' +
                            '');

                        dialog.off('confirm').on('confirm',function(callback){
                            $.post('/IActivity/withdraw',{
                                'snap_date':me.attr('data-snap-date')
                            },function(ret){
                                if(ret.error_no == 0){
                                    callback();
                                    $(this).removeClass('btn-enable');
                                }
                            },"json");
                        });
                    }
                }
            });

            $("body").click(function(){
                $bankList.hide();
            });

            $bankList.click(function(e){
                e.stopPropagation();
            })
        },

        showBankList:function(){
            var noBankTpl = '您还没有添加银行卡，<a href="/user/withdraw">去添加</a>';
            $.get('/bank/getbankcard',function(ret,status){
                 if(ret.error_no == 0){
                     var html = '<ul>',
                         list = ret.data;
                     if(list.length){
                         $.each(list,function(k,v){
                             html+='<li data-id="'+v['id']+'" data-bank-number="'+ v["bankcard_number"]+'">'+ v['bankcard_number']+'</li>'
                         });
                         html+='</ul>';
                         $bankList.html(html).show().find('li').click(function(e){
                             var bankId = $(this).attr('data-id'),
                                 bankcardNumber = $(this).attr('data-bank-number');
                             $.post("/iActivity/bindbank",{
                                 bank_id:bankId
                             },function(ret,status){
                                 if(ret.error_no == 0){
                                     $headerContent.find('.item3 .data-value').text(bankcardNumber);
                                     $headerContent.find('.item3 .data-name a').text('修改');
                                     $bankList.hide();
                                 }
                             },"json");
                         });
                     }else{
                         $bankList.html(noBankTpl);
                     }
                 }else{
                     $bankList.html('获取银行卡信息失败')
                 }
            },"json")
        }
    });
})();